﻿
Imports osi.root.delegates
Imports osi.root.lock

Namespace stopwatch
    Public Module _stopwatch_repeat
        Public Function repeat(ByVal waitms As UInt32,
                               ByVal d As _do(Of Boolean)) As Boolean
            If d Is Nothing Then
                Return False
            Else
                Dim e As stopwatch_event = Nothing
                Dim r As Boolean = False
                e = New stopwatch_event(waitms, Sub()
                                                    r = d()
                                                End Sub)
                Return queue_runner.push(Function() As Boolean
                                             r = False
                                             If e.do() Then
                                                 Return True
                                             Else
                                                 If r Then
                                                     e.restart()
                                                 End If
                                                 Return r
                                             End If
                                         End Function)
            End If
        End Function

        Public Function repeat(ByVal waitms As UInt32, ByVal d As void) As Boolean
            If d Is Nothing Then
                Return False
            Else
                Return repeat(waitms, Function() As Boolean
                                          d()
                                          Return True
                                      End Function)
            End If
        End Function

        Public Function repeat(ByVal waitms As Int32, ByVal d As void) As Boolean
            Return repeat(_uint32(waitms), d)
        End Function

        Public Function repeat(ByVal waitms As Int64, ByVal d As void) As Boolean
            Return repeat(_uint32(waitms), d)
        End Function

        Public Function repeat(ByVal waitms As Int32, ByVal d As _do(Of Boolean)) As Boolean
            Return repeat(_uint32(waitms), d)
        End Function

        Public Function repeat(ByVal waitms As Int64, ByVal d As _do(Of Boolean)) As Boolean
            Return repeat(_uint32(waitms), d)
        End Function
    End Module
End Namespace
